package jmu.cze.controller;

import jmu.cze.service.QueryService;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.Date;

@Controller
public class QueryController {
    private QueryService queryService;

    @Resource(name = "queryServiceImpl")
    public void setQueryService(QueryService queryService) {
        this.queryService = queryService;
    }

    @RequestMapping("/")
    public String query(Model model) {
        model.addAttribute("cities", queryService.selectAllCity());
        model.addAttribute("date",new Date());
        return "queryFlight";
    }

    @GetMapping("/queryByCity")
    public String queryByCity(String start,
                              String end,
                              @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date,
                              Model model) {
        model.addAttribute("cities", queryService.selectAllCity());
        model.addAttribute("list", queryService.queryByCity(start, end, date));
        model.addAttribute("start",start);
        model.addAttribute("end",end);
        model.addAttribute("date",date);
        return "queryFlight";
    }

    @GetMapping("/queryByFlightNo")
    public String queryByFlightNo(String flightNo,
                                  @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date,
                                  Model model) {
        model.addAttribute("cities", queryService.selectAllCity());
        model.addAttribute("list", queryService.queryByFlightNo(flightNo, date));
        model.addAttribute("flightNo",flightNo);
        model.addAttribute("date",date);
        return "queryFlight";
    }
}
